<?php

class ActionCreateUser extends Action {
	public function IsPermitted(){ return User::GetCurrent()->IsAdministrator(); }
	public function GetIconName(){ return 'oxy/ico/User'; }
	public function GetParentAction(){ return new ActionBrowseUsers(); }

	
	public function Render(){
		
		$x = new User();
		$ui = $x->GetUIObject('new');
		
		$v = new ValidatorSet();		
		if ($this->IsPostback()){
			$ui->Read(Http::$POST);


			$v->Username->CheckMandatory($x->Username);
			if ($v->Username->HasPassed()){
				$xx = User::RetrieveByUsername($x->Username);
				$v->Username->Check($xx==null || $xx->id->AsInt()==$x->id->AsInt(), Lemma::ErrorUsernameExists());
			}

			$pwd = Http::$POST['Password']->AsString();
			$v->Password->CheckMandatory($pwd);

			$pwd2 = Http::$POST['ConfirmPassword']->AsString();
			$v->ConfirmPassword->CheckMandatory($pwd2);
			if ($v->ConfirmPassword->HasPassed())
				$v->ConfirmPassword->Check($pwd == $pwd2, Lemma::ErrorPasswordsDoNotMatch());

			if ($v->HasPassed()){
				$x->idUserGroup = UserGroup::idAdministrators;
				$x->Class = 'User';
				$x->Save();
				Oxygen::Redirect($this->GetParentAction());
				return;
			}
		}			
			
		
		echo '<br/>';
		echo $this->GetForm();
		$z = FieldTableControl::Make();
		$z->Add(TextboxControl::Make($ui->Username))->WithValidator($v->Username);
		$z->Add(TextboxControl::Make($ui->Email));
		$z->Add(Lemma::Password())->Write(PasswordControl::Make('Password'))->WithValidator($v->Password);
		$z->Add(Lemma::ConfirmPassword())->Write(PasswordControl::Make('ConfirmPassword'))->WithValidator($v->ConfirmPassword);
		$z->Add(ButtonControl::Make()->WithIsSubmit(true)->WithValue(Lemma::OK()));
		$z->Render();
		echo $this->EndForm();

		
		
		
		
	}
}

?>
